<template>
    <div>
        <!--   社会救助   -->
        <el-form :inline="true" :model="form" class="demo-form-inline">
            <el-form-item>
                <el-input clearable v-model="form.project_name" style="width: 300px" placeholder="申报单位"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="init">搜索</el-button>
            </el-form-item>
            <el-form-item>
                <el-button type="success" @click="create">创建</el-button>
            </el-form-item>
        </el-form>
        <!--           11 列表-->
        <el-table :data="tableData" border >
            <el-table-column
                    fixed
                    prop="unit"
                    label="申报单位"
                    align="center">
            </el-table-column>
            <el-table-column
                    prop="project_name"
                    label="项目名称"
                    align="center">
            </el-table-column>
            <el-table-column
                    prop="state"
                    label="申报状态"
                    align="center">
                <template slot-scope="scope">
                    <span v-if="scope.row.state == '0'">未申报</span>
                    <span v-if="scope.row.state == '1'">申报中</span>
                    <span v-if="scope.row.state == '2'">已评价</span>
                    <span v-if="scope.row.state == '3'">已结束</span>
                </template>
            </el-table-column>
            <el-table-column
                    fixed="right"
                    label="操作"
                    align="center"
                   >
                <template slot-scope="scope">
                    <el-button
                            v-show="scope.row.state==='0'"
                            type="primary"
                            size="mini"
                            @click="release(scope.row)">申报
                    </el-button>
                    <el-button
                            v-show="scope.row.state!=='3'"
                            type="success"
                            size="mini"
                            @click="handleEdit(scope.row)">编辑
                    </el-button>
                    <el-button
                            v-show="scope.row.state==='3'"
                            type="success"
                            size="mini"
                            @click="detail(scope.row)">详情
                    </el-button>
                    <el-button
                            type="danger"
                            size="mini"
                            @click="handleDelete(scope.row)">删除
                    </el-button>
                    <el-button
                            v-show="scope.row.state !== '3'"
                            type="warning"
                            size="mini"
                            @click="stop(scope.row)">停止
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
<!--        分页-->
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="pageNo"
                :page-sizes="[10, 20, 50, 100]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next"
                :total="totalRecord" class="text-center mt10">
        </el-pagination>
<!--        创建弹出框-->
        <el-dialog title="创建" :visible.sync="dialogFormVisible">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="180px" class="demo-ruleForm">
                <el-form-item label="申报单位" prop="unit">
                    <div style="width: 320px;margin-left: 10px">
                        <el-input v-model="ruleForm.unit"></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="项目名称" prop="project_name">
                    <div style="width: 320px;margin-left: 10px">
                        <el-input v-model="ruleForm.project_name"></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="项目类型" prop="project_type">
                    <div style="width: 320px;margin-left: 10px">
                        <el-input v-model="ruleForm.project_type"></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="项目经费" prop="project_funding">
                    <div style="width: 320px;margin-left: 10px">
                        <el-input v-model="ruleForm.project_funding"></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="服务人群" prop="service_people">
                    <div style="width: 320px;margin-left: 10px">
                        <el-input v-model="ruleForm.service_people"></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="申报理由" prop="reasons">
                    <div style="width: 320px;margin-left: 10px">
                        <el-input type="textarea" v-model="ruleForm.reasons"></el-input>
                    </div>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="sure">确 定</el-button>
            </div>
        </el-dialog>

<!--        修改弹出框-->
        <el-dialog title="修改" :visible.sync="dialogFormVisibleEdit">
            <el-form :model="ruleFormEdit" :rules="rulesEdit" ref="ruleForm" label-width="180px" class="demo-ruleForm">
                <el-form-item label="申报单位" prop="unit">
                    <div style="width: 320px;margin-left: 10px">
                        <el-input v-model="ruleFormEdit.unit"></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="项目名称" prop="project_name">
                    <div style="width: 320px;margin-left: 10px">
                        <el-input v-model="ruleFormEdit.project_name"></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="项目类型" prop="project_type">
                    <div style="width: 320px;margin-left: 10px">
                        <el-input v-model="ruleFormEdit.project_type"></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="项目经费" prop="project_funding">
                    <div style="width: 320px;margin-left: 10px">
                        <el-input v-model="ruleFormEdit.project_funding"></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="服务人群" prop="service_people">
                    <div style="width: 320px;margin-left: 10px">
                        <el-input v-model="ruleFormEdit.service_people"></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="申报理由" prop="reasons">
                    <div style="width: 320px;margin-left: 10px">
                        <el-input type="textarea" v-model="ruleFormEdit.reasons"></el-input>
                    </div>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisibleEdit = false">取 消</el-button>
                <el-button type="primary" @click="yes">确 定</el-button>
            </div>
        </el-dialog>

        <!--        详情弹出框-->
        <el-dialog title="修改" :visible.sync="detailVisible">
            <el-form :model="detailForm" label-width="180px" class="demo-ruleForm">
                <el-form-item label="申报单位">
                    <div style="width: 320px;margin-left: 10px">
                        <el-input v-model="detailForm.unit" readonly></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="项目名称">
                    <div style="width: 320px;margin-left: 10px">
                        <el-input v-model="detailForm.project_name" readonly></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="项目类型">
                    <div style="width: 320px;margin-left: 10px">
                        <el-input v-model="detailForm.project_type" readonly></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="项目经费">
                    <div style="width: 320px;margin-left: 10px">
                        <el-input v-model="detailForm.project_funding" readonly></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="服务人群">
                    <div style="width: 320px;margin-left: 10px">
                        <el-input v-model="detailForm.service_people" readonly></el-input>
                    </div>
                </el-form-item>
                <el-form-item label="申报理由">
                    <div style="width: 320px;margin-left: 10px">
                        <el-input type="textarea" v-model="detailForm.reasons" readonly></el-input>
                    </div>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="detailVisible = false">关 闭</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>


    export default {

        name: 'test',

        data() {
            return {
                detailForm:{},//详情数据
                tableData: [],
                pageNo: 1,
                pageSize: 10,
                totalRecord: 0,
                totalPage: 0,
                dialogFormVisible: false, //创建弹出框
                dialogFormVisibleEdit:false, //修改弹出框
                detailVisible:false,//详情弹出框
                form:{},  //创建按钮的表单
                //创建表单
                ruleForm: {
                    title: '',
                    content: '',
                },
                //修改表单
                ruleFormEdit:{
                    title: '',
                    content: '',
                },
                //验证
                rules: {
                    unit: [
                        { required: true, message: '请输入标题', trigger: 'blur' }
                    ],
                    project_name: [
                        { required: true, message: '请输入内容', trigger: 'blur' }
                    ],
                    project_type: [
                        { required: true, message: '请输入内容', trigger: 'blur' }
                    ],
                    project_funding: [
                        { required: true, message: '请输入内容', trigger: 'blur' }
                    ],
                    service_people: [
                        { required: true, message: '请输入内容', trigger: 'blur' }
                    ],
                    reasons: [
                        { required: true, message: '请输入内容', trigger: 'blur' }
                    ],
                },
                //修改验证
                rulesEdit: {
                    unit: [
                        { required: true, message: '请输入标题', trigger: 'blur' }
                    ],
                    project_name: [
                        { required: true, message: '请输入内容', trigger: 'blur' }
                    ],
                    project_type: [
                        { required: true, message: '请输入内容', trigger: 'blur' }
                    ],
                    project_funding: [
                        { required: true, message: '请输入内容', trigger: 'blur' }
                    ],
                    service_people: [
                        { required: true, message: '请输入内容', trigger: 'blur' }
                    ],
                    reasons: [
                        { required: true, message: '请输入内容', trigger: 'blur' }
                    ],
                },
                formLabelWidth: '120px',
                cid:''
            }
        },

        props: {},

        components: {},

        computed: {},

        methods: {
            // 初始化
            async init() {
                const res = await this.$http('selectList',{
                    pageNo: this.pageNo,
                    pageSize: this.pageSize,
                    ...this.form
                })
                this.tableData = res.results
                this.totalPage = res.totalPage;
                this.totalRecord = res.totalRecord;
                //console.log(res)
                //this.form = {}
            },
            handleClick(row) {
                console.log(row);
            },
            //分页
            async handleSizeChange(val) {
                this.pageSize = val;
                this.init();
            },
            async handleCurrentChange(val) {
                this.pageNo = val;
                this.init();
            },
            //创建
            create() {
                this.dialogFormVisible = true
                this.ruleForm.title = ""
                this.ruleForm.content = ""
            },
            //点击确定立即创建
            async sure(){
                const res = await this.$http('createassistance',{
                    unit:this.ruleForm.unit,
                    project_name:this.ruleForm.project_name,
                    project_type:this.ruleForm.project_type,
                    project_funding:this.ruleForm.project_funding,
                    service_people:this.ruleForm.service_people,
                    reasons:this.ruleForm.reasons
                })
                if (res.count === 1) {
                    this.$message({
                        message: '创建成功',
                        type: 'success'
                    });
                    this.dialogFormVisible = false
                    this.init()
                } else {
                    this.$message({
                        message: '创建失败',
                        type: 'error'
                    });
                }
            },
            //详情
            async detail(parme){
                this.detailVisible = true
                this.detailForm = parme
            },
            //编辑
            handleEdit(row){
                this.dialogFormVisibleEdit = true
                this.cid = row.id
                this.ruleFormEdit = row
            },
            //点击确定即可完成编辑
            async yes(){
                const res = await this.$http('createassistance',{
                    id:this.cid,
                    unit:this.ruleFormEdit.unit,
                    project_name:this.ruleFormEdit.project_name,
                    project_type:this.ruleFormEdit.project_type,
                    project_funding:this.ruleFormEdit.project_funding,
                    service_people:this.ruleFormEdit.service_people,
                    reasons:this.ruleFormEdit.reasons
                })
                if (res.count === 1) {
                    this.$message({
                        message: '编辑成功',
                        type: 'success'
                    });
                    this.dialogFormVisibleEdit = false
                    this.init()
                } else {
                    this.$message({
                        message: '编辑失败',
                        type: 'error'
                    });
                }
            },
            //删除
            async handleDelete(row){
                this.$confirm('将永久删除该内容, 是否继续?', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(async () => {
                    const res =await this.$http('createassistance',{
                        id:row.id,
                        del_flag:'1'
                    })
                    this.init()
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //申报
            async release(row){
                const  res = await this.$http('updateState1',{
                    id:row.id
                })
                if (res.count === 1) {
                    this.$message({
                        message: '申报成功',
                        type: 'success'
                    });
                    this.init()
                } else {
                    this.$message({
                        message: '申报失败',
                        type: 'error'
                    });
                }
            },
            //停止
            async stop(parme){
                this.$confirm('将停止改项目, 是否继续?', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(async () => {
                    const res =await this.$http('updateend',{
                        id:parme.id
                    })
                    this.init()
                    this.$message({
                        type: 'success',
                        message: '停止成功!'
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消'
                    });
                });
            },
            //搜索
            async query(){
                const res = await this.$http('selectList',{
                    project_name:this.form.project_name
                })
                this.tableData = res.results
                this.totalPage = res.totalPage;
                this.totalRecord = res.totalRecord;
            }
        },

        watch: {},

        mounted() {
            this.init();
        },

    }
</script>

<style scoped>

</style>
